﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TaobaoERP.BusinessTaobao.Common;
using TaobaoERP.BusinessTaobao.ParObject;
using System.Collections.Specialized;
using TaobaoERP.Entity;

namespace TaobaoERP.BusinessTaobao.TaobaoAPI
{
    public class ItemAPI : MarshalByRefObject
    {
        /// <summary>
        /// 出售中商品列表
        /// </summary>
        /// <param name="Nick"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        [AlisoftAPI(apiType = 2)]
        public List<Item> ListSaleItem(string Nick, int id)
        {
            OnSaleListPar parSL = new OnSaleListPar();
            parSL.fields = Nick;
            return APIUtils.Result<List<Item>>(parSL, (string result) =>
            {
                NameValueCollection par = new NameValueCollection();
                par.Add("ID", "cid");
                par.Add("Name", "name");
                par.Add("ParentID", "parent_cid");
                return new XmlHandler<Item>().ListItemByXml(result, "rsp/seller_cat", par);
            });

        }

        /// <summary>
        /// 库存商品列表
        /// </summary>
        /// <param name="Nick"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        [AlisoftAPI(apiType = 2)]
        public List<Item> ListStockItem(string Nick, int id)
        {
            OnSaleListPar parSL = new OnSaleListPar();
            parSL.fields = Nick;
            return APIUtils.Result<List<Item>>(parSL, (string result) =>
            {
                NameValueCollection par = new NameValueCollection();
                par.Add("ID", "cid");
                par.Add("Name", "name");
                par.Add("ParentID", "parent_cid");
                return new XmlHandler<Item>().ListItemByXml(result, "rsp/seller_cat", par);
            });

        }

        /// <summary>
        /// 此接口方法用以得到单个商品信息。
        ///注意该api的调用级别,如果需要获取隐私信息(包括商品的隐私字段和仓库内商品),则需附上sip_sessionid.
        ///注：因为业务规则的原因，部分用户的少量商品，无法通过此接口获取
        /// </summary>
        /// <param name="id"></param>
        /// <param name="nick"></param>
        /// <returns></returns>
        [AlisoftAPI(apiType = 3)]
        public Item GetItem(int id,string nick)
        {
            ItemPar parSL = new ItemPar();
            parSL.nick = nick;
            parSL.iid = id;
            return APIUtils.Result<Item>(parSL, (string result) =>
            {
                NameValueCollection par = new NameValueCollection();
                //par.Add("ID", "cid");
                //par.Add("Name", "name");
                //par.Add("ParentID", "parent_cid");
                return new XmlHandler<Item>().GetItemByXml(result, "rsp/seller_cat", par);
            });
        }

        /// <summary>
        /// 添加一个商品信息
        /// </summary>
        /// <param name="parSL"></param>
        /// <returns></returns>
        [AlisoftAPI(apiType = 2)]
        public int? add(ItemAddPar parSL)
        {
            return APIUtils.Result<int>(parSL, (string result) =>
            {
                return new XmlHandler<int>().GetItemByXml(result, "rsp/item", "iid");
            });
        }
    }
}
